var path = require('path');
require('dotenv').load();

module.exports = function (grunt) {
    'use strict';
    // 配置项
    var BUILD_CONFIG = {
        //访问路径
        url: 'http://' + process.env.urlAbsoluteDev,
        //源文件目录
        src: {
            frontend: {
                root: 'frontend',
                app: 'frontend/app'
            },
            backend: {
                root: 'backend'
            }
        },
        //发布文件目录
        dist: {
            root: 'dist',
            frontend: {
                root: 'dist/frontend',
                module: 'dist/frontend/app/module',
                bower: 'dist/frontend/lib',
                injector: {
                    bower: {
                        target: 'dist/frontend/index.html',
                        files: '../../bower.json',
                        ignorePath: 'frontend/'
                    },
                    index: {
                        target: 'dist/frontend/index.html',
                        files: [
                            'app/app.module.js',
                            'app/*.js',
                            'app/core/core.module.js',
                            'app/core/**/*.js',
                            'app/layout/layout.module.js',
                            'app/layout/layout.config.js',
                            'app/module/define.module.js',
                            'app/module/**/*.module.js',
                            'app/module/**/*.config.js',
                            'app/module/**/*.run.js'
                        ]
                    },
                    module: {
                        target: 'dist/frontend/app/module/define.module.js',
                        files: 'app/module/**/*.module.js',
                        starttag: '// injector:js',
                        endtag: '// endinjector'
                    },
                    menu: {
                        target: 'dist/frontend/app/layout/header/header.view.html',
                        files: 'app/module/**/*.menu.html',
                        starttag: '<!-- injector:menu -->',
                        endtag: '<!-- endinjector -->'
                    }
                }
            },
            backend: {
                root: 'dist/backend',
                script: 'dist/backend/server.js'
            }
        }
    };

    var menu = grunt.file.readJSON('frontend.menu.json');

    //加载所有的任务
    require('load-grunt-tasks')(grunt);

    //显示每一个任务所花的时间和百分比
    require('time-grunt')(grunt);

    grunt.initConfig({
        config: BUILD_CONFIG,
        //编码规范检查
        jshint: {
            options: {
                reporter: require('jshint-stylish'),
                jshintrc: '.jshintrc'
            },
            script: ['Gruntfile.js'],
            frontend: ['<%= config.src.frontend.app %>/**/*.js'],
            backend: ['<%= config.src.backend.root %>/**/*.js']
        },
        //清理输出目录
        clean: {
            frontend: {
                files: [{
                    dist: '<%= config.dist.frontend.root %>'
                }]
            },
            backend: {
                files: [{
                    dist: '<%= config.dist.backend.root %>'
                }]
            }
        },
        //复制文件
        copy: {
            frontend: {
                files: [
                    {
                        //前端根目录下文件
                        expand: true,
                        cwd: '<%= config.src.frontend.root %>',
                        src: ['**'],
                        dest: '<%= config.dist.frontend.root %>',
                        filter: 'isFile'
                    }
                ]
            },
            backend: {
                files: [
                    {
                        //后端文件
                        expand: true,
                        cwd: '<%= config.src.backend.root %>',
                        src: ['**'],
                        dest: '<%= config.dist.backend.root %>',
                        filter: 'isFile'
                    }
                ]
            }

        },
        injector: {
            options: {
                addRootSlash: false,
                bowerPrefix: 'bower:'
            },
            bower: {
                options: {
                    destFile: '<%= config.dist.frontend.injector.bower.target %>',
                    ignorePath: ['<%= config.dist.frontend.injector.bower.ignorePath %>']
                },
                files: [{
                    expand: true,
                    cwd: '<%= config.dist.frontend.root %>',
                    dest: '<%= config.dist.frontend.root %>',
                    src: '<%= config.dist.frontend.injector.bower.files %>'
                }]
            },
            index: {
                options: {
                    destFile: '<%= config.dist.frontend.injector.index.target %>',
                    ignorePath: ['<%= config.dist.frontend.root %>']
                },
                files: [{
                    expand: true,
                    cwd: '<%= config.dist.frontend.root %>',
                    dest: '<%= config.dist.frontend.root %>',
                    src: '<%= config.dist.frontend.injector.index.files %>'
                }]
            },
            module: {
                options: {
                    destFile: '<%= config.dist.frontend.injector.module.target %>',
                    ignorePath: ['<%= config.dist.root %>'],
                    starttag: '<%= config.dist.frontend.injector.module.starttag %>',
                    endtag: '<%= config.dist.frontend.injector.module.endtag %>',
                    transform: function (filepath, i, length) {
                        var moduleName = path.basename(filepath, '.module.js');
                        if (moduleName === 'define') {
                            return '';
                        } else {
                            if (i === (length - 1)) {
                                return '\'module.' + path.basename(filepath, '.module.js') + '\',';
                            } else {
                                return '\'module.' + path.basename(filepath, '.module.js') + '\',';
                            }
                        }
                    }
                },
                files: [{
                    expand: true,
                    cwd: '<%= config.dist.frontend.root %>',
                    dest: '<%= config.dist.frontend.root %>',
                    src: ['<%= config.dist.frontend.injector.module.files %>']
                }]
            },
            menu: {
                options: {
                    destFile: '<%= config.dist.frontend.injector.menu.target %>',
                    starttag: '<%= config.dist.frontend.injector.menu.starttag %>',
                    endtag: '<%= config.dist.frontend.injector.menu.endtag %>',
                    transform: function (filepath, i, length) {
                        return grunt.file.read(filepath).toString();
                    },
                    sort: function (a, b) {
                        if (menu.order.indexOf(path.basename(a, '.menu.html')) < menu.order.indexOf(path.basename(b, '.menu.html'))) {
                            return -1;
                        } else {
                            return 1;
                        }
                    }
                },
                files: [{
                    expand: true,
                    cwd: '<%= config.dist.frontend.root %>',
                    dest: '<%= config.dist.frontend.root %>',
                    src: ['<%= config.dist.frontend.injector.menu.files %>']
                }]
            }
        },
        //启动Express
        express: {
            run: {
                options: {
                    background: true,
                    script: '<%= config.dist.backend.script %>',
                    //jshint camelcase:false
                    node_env: 'development'
                }
            }
        },
        //启动浏览器,访问页面
        open: {
            build: {
                //jshint camelcase:false
                path: '<%= config.url %>'
            }
        },
        //文件变化监控
        watch: {
            options: {
                livereload: true
            },
            base: {
                files: ['bower.json', 'package.json']
            },
            frontend: {
                files: ['<%= config.src.frontend.root %>/**/*.*'],
                tasks: [
                    'jshint:frontend',
                    'clean:frontend',
                    'copy:frontend',
                    'injector'
                ]
            },
            backend: {
                files: ['<%= config.src.backend.root %>/**/*.*'],
                tasks: [
                    'jshint:backend',
                    'clean:backend',
                    'copy:backend',
                    'express'
                ],
                options: {
                    spawn: false,
                    interrupt: true
                }
            }
        }
    });

    //默认启动研发环境
    grunt.registerTask('default', ['dev']);
    grunt.registerTask('dev', [
        'build', 'express', 'open', 'watch'
    ]);

    //构建
    grunt.registerTask('build', [
        'jshint', 'clean', 'copy', 'injector'
    ]);

    //执行
    grunt.registerTask('run', [
        'express', 'open', 'watch'
    ]);
};
